<template>
    <div class="goods-container">
        <transition
          @before-enter="beforEnter"
          @enter="enter"
          @after-enter="afterEnter"
        >
            <div class="ball" v-show="ballFlag" ref="ball"></div>
        </transition>
        <!-- 商品轮播图区域-->
        <div class="mui-card">
				<div class="mui-card-content">
					<div class="mui-card-content-inner">
						<swiper :lunbotuList="lunbotuList" :isfull="false"></swiper>
					</div>
				</div>
			</div>

            <!-- 商品购买区域 -->
            <div class="mui-card">
				<div class="mui-card-header">{{ mes.name }}</div>
				<div class="mui-card-content">
					<div class="mui-card-content-inner">
						<p class="price">
                            市场价：<del>￥{{ mes.oldP }}</del>&nbsp;&nbsp;销售价：<span class="now_price">￥{{ mes.newP }}</span>
                        </p>
                        <p>
                            购买数量：<numberbox @getSelectedCount="getSelectedCount" :maxcount="mes.kcqk"></numberbox>
                        </p>
                        <p>
                            <mt-button type="primary" size="small">立即购买</mt-button>
                            <mt-button type="danger" size="small" @click="addCar">加入购物车</mt-button>
                        </p>
					</div>
				</div>
			</div>

            <!-- 商品参数区域 -->
            <div class="mui-card">
				<div class="mui-card-header">商品参数</div>
				<div class="mui-card-content">
					<div class="mui-card-content-inner">
						<p>商品货号：{{ mes.sphh }}</p>
                        <p>库存情况：{{ mes.kcqk }}</p>
                        <p>上架时间：{{ mes.sjsj }}</p>
					</div>
				</div>
				<div class="mui-card-footer">
                    <mt-button type="primary" size="large" plain @click="goDesc(id)">图文介绍</mt-button>
                    <mt-button type="danger" size="large" plain @click="goComment(id)">商品评论</mt-button>
                </div>
			</div>
    </div>
</template>

<script>
import swiper from '../subcomponents/swiper.vue'
import numberbox from '../subcomponents/goodsInfo_numberBox.vue'
export default {
    data(){
        return {
            id: this.$route.params.id,
            lunbotuList: [],
            mes: {},
            ballFlag: false, // 控制小球显示和隐藏
            selectedCount: 1,
        }
    },
    methods: {
        getSelectedCount(count){ // 获取选购的数量
            this.selectedCount = count
        },
        beforEnter(el){
        el.style.transform = "translate(0, 0)"
        },
        enter(el, done){
            el.offsetWidth
            // 1.小球最终的 到达的位置不能写死
            // 2.动态计算坐标值
            // 3. 购物车的纵轴 - 小球的纵轴
            // 4. 购物车的横轴 - 小球的横轴
            // 5. domObject.getDBoundingClientRect()
            // 获取小球在页面中的位置
            const ballPosition = this.$refs.ball.getBoundingClientRect()
            // 获取购物车的位置
            const carPosition = document.getElementById("badge").getBoundingClientRect()
            const x = carPosition.left - ballPosition.left
            var y = carPosition.top - ballPosition.top
            el.style.transform = `translate(${x}px, ${y}px)`
            // el.style.transform = "translate(100px, 300px)"
            el.style.transition = 'all 0.5s cubic-bezier(.4,-0.3,1,.68)'
            done()
        },
        afterEnter(el){
            this.ballFlag = !this.ballFlag
        },
        addCar(){ //添加到购物车
            this.ballFlag = !this.ballFlag
            // 拼接出一个要保存到 store.state 中 car 的数据对象
            var goodsinfo = { 
                id: this.id, 
                count: this.selectedCount, 
                price: this.mes.newP, 
                img: this.mes.img,
                name: this.mes.name,
                selected: true
            }
            // 调用 store 中的 mutations 中的 addToCar 来保存加购的商品信息
            this.$store.commit('addToCar', goodsinfo)
        },
        goComment(id){ //评论页面
            this.$router.push({name: 'goodscomment' ,params:{ id }})

        },
        goDesc(id){ //评论页面
            this.$router.push({name: 'goodsdesc' ,params:{ id }})

        },
        getLunbotu() { // 获取轮播图数据的方法
                if(this.id=='1'){
                   this.lunbotuList = [{
                    img: '../../images/sj-1.jpg'
                    }, {
                        img: '../../images/sj-2.jpg'
                    }, {
                        img: '../../images/sj-3.jpg'
                    }, {
                        img: '../../images/sj-4.jpg'
                    }, ]    
                }else if(this.id=='2'){
                        this.lunbotuList = [{
                    img: '../../images/sj-5.jpg'
                    },  {
                        img: '../../images/sj-6.jpg'
                    }, {
                        img: '../../images/sj-7.jpg'
                    }, ]
                }else if(this.id=='3'){
                        this.lunbotuList = [{
                    img: '../../images/0.jpg'
                    },  {
                        img: '../../images/5.jpg'
                    }, {
                        img: '../../images/6.jpg'
                    }, ]
                }


            },
            getMessage(){
                 if(this.id=='1'){
                   this.mes = {
                     name: 'iphone 最新款 爆款 抢手款 2020 春节限定',
                     oldP: 2399,
                     newP: 2099,
                     sphh: 'HSH-2748HHDjhJ-fja889',
                     kcqk: 50,
                     img: '../../images/sj-3.jpg',
                     sjsj: '2020-01-01'
                    }    
                }else if(this.id=='2'){
                    this.mes = {
                        name: '华为 最新款 爆款 抢手款 5G 春节限定',
                        oldP: 3599,
                        newP: 3099,
                        sphh: 'HSH-jfshfsh27483-fja889',
                        kcqk: 100,
                        img: '../../images/sj-1.jpg',
                        sjsj: '2019-12-21'
                    }
                }else if(this.id=='3'){
                    this.mes = {
                        name: '小米 (MI) 最新款 爆款 抢手款 2020 春节限定',
                        oldP: 2199,
                        newP: 1999,
                        sphh: 'HdhsjfhkUUD-hdj9',
                        kcqk: 300,
                        img: '../../images/sj-7.jpg',
                        sjsj: '2020-01-06'
                    }
                }
            }
    },
    created(){
        this.getLunbotu()
        this.getMessage()
    },
    components: {
            swiper,
            numberbox
    }
}
</script>

<style lang="scss" scoped>
.goods-container{
    background-color: #eee;
    overflow: hidden;
    .now_price{
        color: red;
        font-size: 16px;
        font-weight: bold;
    }
}
.mui-card-footer{
    display: block;
    button{
        margin: 15px 0;
    }
}
.ball{
    width: 15px;
    height: 15px;
    border-radius: 50%;
    background-color: red;
    position: absolute;
    z-index: 99;
    top: 390px;
    left: 146px;
}
</style>